
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/webfg/

//
// 091201 - aha42 - Created.
// 091206 - aha42 - Moved code to WebfgUtil, AbstractWebfgPageCreator and
//                  DefaultWfgPageCreator.
// 091210a- aha42 - Calls m_pageCreator.handleError(fx, resp);
// 091210b- aha42 - Put in the correct URL to project page above.
// 091213 - aha42 - Now forwards ServletContext to WebPageCreator in init and
//                  handles default (no params) request.
//

package org.aha.webfg;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aha.mf4j.FlickrException;
import org.aha.mf4j.FlickrSession;
import org.aha.servlet.RequestParseException;

/**
 * <p>
 *   {@link HttpServlet} generating Flickr Gallery pages.
 * </p>
 * @author Arne Halvorsen (aha42) 
 */
@SuppressWarnings("serial")
public class WebfgServlet extends HttpServlet 
{
  private static final String c_key="a8cb26a65d56ec2eb22fa2f3bb6079fb";
  
  private WebfgPageCreator m_pageCreator;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public WebfgServlet(){}
  
  @Override
  public void init() throws ServletException 
  {
    try 
    {
      m_pageCreator=new DefaultWebfgPageCreator();
      m_pageCreator.init(new FlickrSession(c_key), getServletContext());
    } 
    catch (IOException iox) 
    {
      throw new ServletException(iox);
    }
  }

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException 
  {
    handleReq(req, resp);
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException 
  {
    handleReq(req, resp);
  }

  // Invoked from doGet and doPost.
  public void handleReq(HttpServletRequest req, HttpServletResponse resp) 
    throws IOException
  {
    String where=req.getParameter("where");
    where=(where==null) ? "front" : where;
    
    if (req.getParameterMap().isEmpty())
    {
      m_pageCreator.handleDefaultReq(req, resp);  
    }
    else
    {
      try
      {
             if (where.equals("info"))  m_pageCreator.handleInfoReq(req, resp);
        else if (where.equals("front")) m_pageCreator.handleFrontReq(req, resp);
        else if (where.equals("piece")) m_pageCreator.handlePieceReq(req, resp);
        else
        {
          m_pageCreator.handleUnknownReq(req, resp);
        }
      }
      catch (FlickrException fx)
      {
        m_pageCreator.handleError(fx, resp);
      }
      catch (RequestParseException hrpx)
      {
        m_pageCreator.handleError(hrpx, resp);
      }
    }
  }
    
}
